﻿using System;

namespace Yes.Core.Helper
{
    public static class TimeHelper
    {
        public static DateTime ToUtc(DateTime value)
        {
            if (value.Kind == DateTimeKind.Unspecified)
            {
                return new DateTime(value.Ticks, DateTimeKind.Utc);
            }
            return value.ToUniversalTime();
        }

        /// <summary>
        /// 判断Utc格式时间是否过期
        /// </summary>
        public static bool CheckExpiresUtc(DateTime? dt)
        {
            if (dt == null) { return false; }
            return ToUtc(Convert.ToDateTime(dt)) < ToUtc(DateTime.Now);
        }


        public static string TimeTostring(DateTime? dt, string format = "yyyy-MM-dd hh:mm:ss")
        {
            if (dt == null)
            {
                return "";
            }
            return Convert.ToDateTime(dt).ToString(format);
        }
    }
}
